package com.rayc.mall.address.controller;

import com.rayc.mall.address.service.IAddressService;
import com.rayc.mall.api.dto.AddressDTO;
import com.rayc.mall.api.dto.AddressFormDTO;
import com.rayc.mall.common.constant.Constants;
import com.rayc.mall.common.enums.DefaultFlag;
import com.rayc.mall.common.result.Result;
import com.rayc.mall.common.utils.BeanUtils;
import com.rayc.mall.common.utils.CollUtils;
import com.rayc.mall.core.annotation.CacheAddByUserId;
import com.rayc.mall.core.annotation.CacheRemoveByUserId;
import com.rayc.mall.common.context.UserContext;
import com.rayc.mall.address.domain.po.Address;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-25
 * @Description: 收货地址管理接口
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/addresses")
@RequiredArgsConstructor
@Api(tags = "收货地址管理接口")
public class AddressController {

    private final IAddressService addressService;

    @ApiOperation("查询当前用户地址列表")
    @GetMapping("/list")
    @CacheAddByUserId(key = Constants.REDIS_KEY_ADDRESSES)
    public Result<List<AddressDTO>> queryMyAddresses() {
        List<Address> list = addressService.query().eq("user_id", UserContext.getUser()).list();
        if (CollUtils.isEmpty(list)) {
            return Result.success(CollUtils.emptyList());
        }
        List<AddressDTO> addressDTOS = BeanUtils.copyList(list, AddressDTO.class);
        return Result.success(addressDTOS);
    }

    @ApiOperation("新增地址信息")
    @PostMapping("/add")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ADDRESSES)
    public Result<String> saveAddress(AddressFormDTO addressFormDTO) {
        addressFormDTO.setId(null); // id自增
        Address address = BeanUtils.copyBean(addressFormDTO, Address.class);
        address.setIsDefault(DefaultFlag.NOT_DEFAULT); // 默认不是默认地址
        address.setUserId(UserContext.getUser());
        ; // 添加的是自己的地址
        addressService.save(address);
        return Result.success();
    }

    @ApiOperation("更新地址默认地址状态")
    @PutMapping("/status/{id}/{isDefault}")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ADDRESSES)
    public Result<String> updateAddressStatus(@PathVariable("id") Long id, @PathVariable("isDefault") Boolean isDefault) {
        log.info("更新地址默认地址状态 id: {}, isDefault: {}", id, isDefault);
        addressService.updateIsDefault(id, isDefault);
        log.info("更新地址默认地址状态成功 id: {}, isDefault: {}", id, isDefault);
        return Result.success();
    }

    @ApiOperation("更新地址")
    @PutMapping("/update")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ADDRESSES)
    public Result<String> updateAddress(AddressFormDTO addressDTO) {
        // 不能修改是否是默认地址，AddressFormDTO没有isDefault属性
        log.info("更新地址信息 {}", addressDTO);
        Address address = BeanUtils.copyBean(addressDTO, Address.class);
        addressService.updateById(address);
        log.info("更新地址信息成功 {}", address);
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping("/delete/{id}")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ADDRESSES)
    public Result<String> deleteAddressById(@PathVariable("id") Long id) {
        log.info("删除地址 id: {}", id);
        addressService.removeById(id);
        log.info("删除地址成功 id: {}", id);
        return Result.success();
    }

    @ApiOperation("根据id查询地址")
    @GetMapping
    public Result<AddressDTO> queryById(@RequestParam Long id) {
        log.info("根据id查询地址 id: {}", id);
        Address address = addressService.getById(id);
        log.info("查询到id为{}的地址信息: {}", id, address);
        return Result.success(BeanUtils.copyBean(address, AddressDTO.class));
    }
}
